package com.leoman.controller.api;

import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.core.ErrorCode;
import com.leoman.core.bean.Result;
import com.leoman.entity.Area;
import com.leoman.entity.Question;
import com.leoman.entity.Type;
import com.leoman.service.AreaService;
import com.leoman.service.ImageService;
import com.leoman.service.QuestionService;
import com.leoman.service.TypeService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by wangbin on 2015/7/29.
 */
@Controller
@RequestMapping(value = "api/question")
public class QuestionApiController extends CommonController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ImageService imageService;

    /**
     * @api {post} /api/question/typeList 问题分类列表
     * @apiName question.typeList
     * @apiGroup question
     * @apiSuccess {Object}   list         问题分类列表
     * @apiSuccess {Integer}  list.id      分类id
     * @apiSuccess {String}   list.name    分类名称
     */
    @RequestMapping(value = "/typeList")
    public void typeList(HttpServletResponse response) {
        List<Type> list = typeService.findAll();

        Result obj = new Result(true).data(list);
        String result = JsonUtil.obj2ApiJson(obj, "isCut");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/question/areaList 区域列表（父级区域）
     * @apiName question.areaList
     * @apiGroup question
     *
     * @apiSuccess {Object}   list         区域列表
     * @apiSuccess {Integer}  list.id      区域id
     * @apiSuccess {String}   list.name    区域名称
     */
    @RequestMapping(value = "/areaList")
    public void areaList(HttpServletResponse response) {
        List<Area> list = areaService.findParentList();

        Result obj = new Result(true).data(list);
        String result = JsonUtil.obj2ApiJson(obj, "isCut", "parentName", "parentId", "size", "level", "type");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/question/areaListPlus 区域列表（子级区域）
     * @apiName question.areaListPlus
     * @apiGroup question
     *
     * @apiParam   {Integer}  areaId       区域id            <必传/>
     *
     * @apiSuccess {Object}   list         区域列表
     * @apiSuccess {Integer}  list.id      区域id
     * @apiSuccess {Integer}  list.size    区域总层数
     * @apiSuccess {Integer}  list.level   区域当前层数（如果当前层数等于总层数，则代表当前区域为最下级区域）
     * @apiSuccess {String}   list.name    区域名称
     */
    @RequestMapping(value = "/areaListPlus")
    public void areaListPlus(HttpServletResponse response, Integer areaId) {
        if (null == areaId) {
            WebUtil.printJson(response, new Result(false).msg(ErrorCode.ERROR_CODE_0002));
            return;
        }

        List<Area> list = areaService.findList(areaId);

        Result obj = new Result(true).data(list);
        String result = JsonUtil.obj2ApiJson(obj, "isCut", "parentName", "parentId", "type");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/question/add 添加反馈
     * @apiName question.add
     * @apiGroup question
     * @apiParam {Integer}   typeId                    问题分类ID                  <必传 />
     * @apiParam {String}    recordDate                记录日期                    <必传 />
     * @apiParam {Integer}   areaId                    区域ID                     <必传 />
     * @apiParam {String}    descriptionOne            设备/设施作业描述            <必传 />
     * @apiParam {String}    descriptionTwo            风险描述                    <必传 />
     * @apiParam {Integer}   scoreOne                  发现区域得分                <必传 />
     * @apiParam {Integer}   scoreTwo                  专业程度得分                <必传 />
     * @apiParam {Integer}   scoreThree                关键程度得分                <必传 />
     * @apiParam {Integer}   scoreFour                 事故可能性得分              <必传 />
     * @apiParam {Integer}   scoreFive                 法规要求得分                <必传 />
     * @apiParam {Integer}   scoreSix                  计算得分                    <必传 />
     * @apiParam {Integer}   scoreSeven                实际得分                    <必传 />
     * @apiParam {String}    nameOne                   反馈人名称                  <必传 />
     * @apiParam {String}    sectionOne                反馈人所属部门               <必传 />
     * @apiParam {String}    descriptionThree          临时措施描述                 <必传 />
     * @apiParam {Integer}   scoreEight                临时措施判别得分             <必传 />
     * @apiParam {Integer}   scoreNine                 临时措施计算得分             <必传 />
     * @apiParam {Integer}   scoreTen                  临时措施实际得分             <必传 />
     * @apiParam {String}    nameTwo                   临时措施任务执行             <必传 />
     * @apiParam {String}    sectionTwo                临时措施执行人责任部门        <必传 />
     * @apiParam {String}    planDateOne               临时措施计划完成日期          <必传 />
     * @apiParam {String}    actualDateOne             临时措施实际关闭日期          <必传 />
     * @apiParam {String}    descriptionFour           纠正措施描述                 <必传 />
     * @apiParam {Integer}   scoreEleven               纠正措施判别得分             <必传 />
     * @apiParam {Integer}   scoreTwelve               纠正措施计算得分             <必传 />
     * @apiParam {Integer}   scoreThirteen             纠正措施实际得分             <必传 />
     * @apiParam {String}    nameThree                 纠正措施任务执行人           <必传 />
     * @apiParam {String}    sectionThree              纠正措施执行人责任部门        <必传 />
     * @apiParam {String}    planDateTwo               纠正措施计划完成日期          <必传 />
     * @apiParam {String}    actualDateTwo             纠正措施实际关闭日期          <必传 />
     * @apiParam {Stream}    images                    图片组（二级制流文件）        <必传 />
     */
    @RequestMapping(value = "/add")
    public void add(HttpServletResponse response,
                    Integer typeId,
                    String recordDate,
                    Integer areaId,
                    String descriptionOne,
                    String descriptionTwo,
                    Integer scoreOne,
                    Integer scoreTwo,
                    Integer scoreThree,
                    Integer scoreFour,
                    Integer scoreFive,
                    Integer scoreSix,
                    Integer scoreSeven,
                    String nameOne,
                    String sectionOne,
                    String descriptionThree,
                    Integer scoreEight,
                    Integer scoreNine,
                    Integer scoreTen,
                    String nameTwo,
                    String sectionTwo,
                    String planDateOne,
                    String actualDateOne,
                    String descriptionFour,
                    Integer scoreEleven,
                    Integer scoreTwelve,
                    Integer scoreThirteen,
                    String nameThree,
                    String sectionThree,
                    String planDateTwo,
                    String actualDateTwo,
                    MultipartRequest multipartRequest) {
        if (null == typeId || !StringUtils.isNotEmpty(recordDate) || null == areaId ||
                !StringUtils.isNotEmpty(descriptionOne) || !StringUtils.isNotEmpty(descriptionTwo) ||
                null == scoreOne || null == scoreTwo ||
                null == scoreThree || null == scoreFour ||
                null == scoreFive || null == scoreSix ||
                null == scoreSeven || !StringUtils.isNotEmpty(nameOne) ||
                !StringUtils.isNotEmpty(sectionOne) || !StringUtils.isNotEmpty(descriptionThree) ||
                null == scoreEight || null == scoreNine ||
                null == scoreTen || !StringUtils.isNotEmpty(nameTwo) ||
                !StringUtils.isNotEmpty(sectionTwo) || !StringUtils.isNotEmpty(planDateOne) ||
                !StringUtils.isNotEmpty(actualDateOne) || !StringUtils.isNotEmpty(descriptionFour) ||
                null == scoreEleven || null == scoreTwelve || null == scoreThirteen ||
                !StringUtils.isNotEmpty(nameThree) || !StringUtils.isNotEmpty(sectionThree) ||
                !StringUtils.isNotEmpty(planDateTwo) || !StringUtils.isNotEmpty(actualDateTwo)) {
            WebUtil.printJson(response, new Result(false).msg(ErrorCode.ERROR_CODE_0002));
            return;
        }

        // 获取图片组
        List<MultipartFile> fileList = multipartRequest.getFiles("images");
        if (null == fileList || fileList.size() == 0) {
            WebUtil.printJson(response, new Result(false).msg(ErrorCode.ERROR_CODE_0005));
            return;
        }

        Question question = new Question();

        Type type = typeService.getById(typeId);

        if (null == type) {
            WebUtil.printJson(response, new Result(false).msg(ErrorCode.ERROR_CODE_0003));
            return;
        }

        Area area = areaService.getById(areaId);

        if (null == area || area.getLevel() != area.getSize()) {
            WebUtil.printJson(response, new Result(false).msg(ErrorCode.ERROR_CODE_0004));
            return;
        }

        question.setType(type);
        question.setRecordDate(recordDate);
        question.setArea(area);
        question.setDescriptionOne(descriptionOne);
        question.setDescriptionTwo(descriptionTwo);
        question.setScoreOne(scoreOne);
        question.setScoreTwo(scoreTwo);
        question.setScoreThree(scoreThree);
        question.setScoreFour(scoreFour);
        question.setScoreFive(scoreFive);
        question.setScoreSix(scoreSix);
        question.setScoreSeven(scoreSeven);
        question.setNameOne(nameOne);
        question.setSectionOne(sectionOne);
        question.setDescriptionThree(descriptionThree);
        question.setScoreEight(scoreEight);
        question.setScoreNine(scoreNine);
        question.setScoreTen(scoreTen);
        question.setNameTwo(nameTwo);
        question.setSectionTwo(sectionTwo);
        question.setPlanDateOne(planDateOne);
        question.setActualDateOne(actualDateOne);
        question.setDescriptionFour(descriptionFour);
        question.setScoreEleven(scoreEleven);
        question.setScoreTwelve(scoreTwelve);
        question.setScoreThirteen(scoreThirteen);
        question.setNameThree(nameThree);
        question.setSectionThree(sectionThree);
        question.setPlanDateTwo(planDateTwo);
        question.setActualDateTwo(actualDateTwo);
        question.setIsCut(Constant.IS_CUT_NO);
        question.setStatus(Constant.STATUS_000);

        questionService.create(question);

        // 保存反馈问题图片
        imageService.batchAddImage(fileList, question.getId());

        String result = JsonUtil.obj2ApiJson(new Result(true));
        WebUtil.printApi(response, result);
    }
}
